﻿using MCPlatform.Dxf.Utils;
using netDxf;
using netDxf.Entities;
using System.Windows.Controls;
using System.Windows.Media;


namespace MCPlatform.Dxf.Entities
{
    /// <summary>  
    /// 表示一个椭圆路径实体的类，继承自PathEntityBase。  
    /// </summary>  
    public class EllipsePathEntity : PathEntityBase
    {
      

       

        /// <summary>  
        /// 椭圆的旋转角度（以弧度为单位）。  
        /// </summary>  
        public double Rotation { get; private set; }

        /// <summary>  
        /// 椭圆的起始角度（以弧度为单位）。  
        /// </summary>  
        public double StartAngle { get; private set; }

        /// <summary>  
        /// 椭圆的结束角度（以弧度为单位）。  
        /// </summary>  
        public double EndAngle { get; private set; }

        

       
        public Ellipse Ellipse { get; private set; }

        double angleIncrement = Math.PI / 180; // 1度转换为弧度 

        /// <summary>  
        /// 构造函数，通过Ellipse实体初始化EllipsePathEntity的实例。  
        /// </summary>  
        /// <param name="ellipse">源Ellipse实体。</param>  
        public EllipsePathEntity(Ellipse ellipse)
        {
            Ellipse = ellipse;
            // 初始化属性  
            Center = ellipse.Center;
            MajorAxis = ellipse.MajorAxis;
            MinorAxis = ellipse.MinorAxis;
            Rotation = ellipse.Rotation;
            StartAngle = ellipse.StartAngle;
            EndAngle = ellipse.EndAngle;
            Length = CalEllipseLength((double)MajorAxis, (double)MinorAxis);
            StartPoint = new Vector3()
            {
                X = ellipse.Center.X - MajorAxis.Value/2,
                Y = ellipse.Center.Y,
            };

            EndPoint = new Vector3()
            {
                X = ellipse.Center.X + MajorAxis.Value/2,
                Y = ellipse.Center.Y,
            };
            Type = ellipse;
            Handle = ellipse.Handle;
            DrawShape(ellipse);
        }

        public void DrawShape(Ellipse? ellipse, float scaleX = 1, float scaleY = 1, float strokeThickness = 1f)
        {
            if (ellipse is null)
            {
                ellipse = this.Ellipse;
            }

            // 创建并配置椭圆形状
            System.Windows.Shapes.Ellipse ellipseShape = new System.Windows.Shapes.Ellipse
            {
                Width = ellipse.MajorAxis , // WPF中的宽度是直径
                Height = ellipse.MinorAxis, // WPF中的高度是直径
               
                Stroke = Utility.ConvertAciColorToBrush(ellipse.Color),
                StrokeThickness = Utility.GetNumberFromTextToFloat(ellipse.Lineweight.ToString(), ellipse.Layer.Lineweight.ToString()) ?? strokeThickness,
            };

            // 设置椭圆的位置
            Canvas.SetLeft(ellipseShape, ellipse.Center.X - ellipse.MajorAxis/2);
            Canvas.SetTop(ellipseShape, ellipse.Center.Y - ellipse.MinorAxis/2);

            this.Shape = ellipseShape;

            this.Shape.Tag = ellipse.Handle;
        }
      
        public double CalEllipseLength(double majorAxis,double minorAxis)
        {
            return 2 * Math.PI * minorAxis + 4 * (majorAxis - minorAxis);
        }
    }
}

